package com.question.controller.admin;

import com.question.pojo.vo.*;
import com.question.pojo.result.PageResult;
import com.question.pojo.result.Result;
import com.question.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/manage/comments")
@Slf4j
@Api(tags = "评论管理相关接口")
public class adminCommentController {

    @Autowired
    private CommentService commentService;

    @ApiOperation("分页查询评论")
    @GetMapping("/page")
    public Result<PageResult> page(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Integer majorId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        PageResult result = commentService.getAdminCommentPage(keyword, majorId, pageNum, pageSize);
        return Result.success(result);
    }

    @ApiOperation("获取专业评论分布")
    @GetMapping("/major-distribution")
    public Result<List<MajorDistributionVO>> getMajorDistribution() {
        return Result.success(commentService.getMajorDistribution());
    }

    @ApiOperation("获取高频评论题目")
    @GetMapping("/hot-questions")
    public Result<List<HotQuestionVO>> getHotQuestions(
            @RequestParam(defaultValue = "10") Integer limit) {

        return Result.success(commentService.getHotQuestions(limit));
    }

    @ApiOperation("删除评论")
    @DeleteMapping("/{commentId}")
    public Result<String> delete(@PathVariable Integer commentId) {
        commentService.deleteCommentById(commentId);
        return Result.success("评论删除成功");
    }

    @ApiOperation("获取评论点赞用户")
    @GetMapping("/{commentId}/likes")
    public Result<List<LikeUserVO>> getLikes(@PathVariable Integer commentId) {
        return Result.success(commentService.getLikeUsers(commentId));
    }

    @ApiOperation("获取评论统计数据")
    @GetMapping("/stats")
    public Result<Map<String, Object>> getCommentStats() {
        Map<String, Object> stats = commentService.getCommentStats();
        return Result.success(stats);
    }

    @ApiOperation("分页查询用户统计")
    @GetMapping("/user-stats/page")
    public Result<PageResult> getUserStatPage(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Integer majorId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        PageResult result = commentService.getUserStatPage(keyword, majorId, pageNum, pageSize);
        return Result.success(result);
    }

    @ApiOperation("获取专业活跃度")
    @GetMapping("/major-activity")
    public Result<List<MajorActivityVO>> getMajorActivity() {
        try {
            List<MajorActivityVO> list = commentService.getMajorActivity();

            // 过滤无效数据
            list = list.stream()
                    .filter(item -> item.getAvgComments() > 0)
                    .collect(Collectors.toList());

            return Result.success(list);
        } catch (Exception e) {
            log.error("获取专业活跃度失败", e);
            return Result.error("获取专业活跃度失败");
        }
    }
}